﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO.Ports;

namespace CRM_SMS_MARKETING.PL.System_Management
{

    public partial class frmModems : DevExpress.XtraEditors.XtraForm
    {
        //Khai Bao Mang Chua Danh Sach Port cua may tinh
        public class Modem
        {
            private int id;
            public int ID
            {
                get { return id; }
                set { id = value; }
            }
            private string port;
            public string Port
            {
                get { return port; }
                set { port = value; }
            }
            private Boolean status;
            public Boolean Status
            {
                get { return status; }
                set { status = value; }
            }
            private string successAndFail;
            public string SuccessAndFail
            {
                get { return successAndFail; }
                set { successAndFail = value; }
            }
        }
        String[] ArrayPort;
        List<Modem> ListPort;

        List<SmsControl.SmsControl> list_Modem;
        public frmModems(List<SmsControl.SmsControl> List_Modem)
        {
            InitializeComponent();
            list_Modem = List_Modem;
        }

        private List<Modem> GetGridViewPort()
        {
            ArrayPort = SerialPort.GetPortNames();

            ListPort = new List<Modem>();


            for (int i = 0; i < ArrayPort.Length; i++)
            {
                Modem modem = new Modem();
                modem.ID = i;
                modem.Port = ArrayPort[i];
                modem.Status = false;
                modem.SuccessAndFail = "0/0";
                ListPort.Add(modem);
            }

            return ListPort;
        }

        private void frmModems_Load(object sender, EventArgs e)
        {

            GetGridViewPort();
            gridControlPort.DataSource = ListPort;
        }

        private void btnConnect_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            try
            {
                SmsControl.SmsControl buffer = new SmsControl.SmsControl();
                buffer.Initialize(gridViewPort.GetFocusedRowCellValue("Port").ToString());

                if (buffer.IsConnect())
                {
                    for (int i = 0; i < ListPort.Count; i++)
                    {
                        if (ListPort[i].Port == buffer.PortName)
                        {
                            ListPort[i].Status = true;

                            gridViewPort.SetFocusedRowCellValue("Status", true);
                            break;
                        }
                    }
                    buffer.NewMessageReceived += new SmsControl.SmsControl.NewMessageReceivedEventHandler(bll_ReceivingAndReplyMessage.Add_New_Message_To_DB);
                    list_Modem.Add(buffer);

                    //Gắn sự kiện nhận
                    
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        BLL_ReceivingAndReplyMessage bll_ReceivingAndReplyMessage = new BLL_ReceivingAndReplyMessage();
    }
}
